<!--
 * @Descripttion:  为保留旧版路径，文件路径只能这样写了
 * @version: 
 * @Author:Hui
 * @Date: 2023-10-08 15:26:04
 * @LastEditTime: 2025-08-18 10:57:51
 * @LastEditors: sueRimn
-->

<template>
  <view class="page">
    <!-- #ifdef H5 -->
    <custom-navbar title="订单详情" />
    <!-- #endif -->
    <!-- 订单状态 -->
    <order-status :textStatus="orderDetail.orderStatusText" :descStatus="orderDetail.orderStatusDesc"/>

    <!-- 用户地址信息 -->
    <!-- <user-info :orderAddress="orderDetail.orderAddress || {}" /> -->

    <!-- 手机信息:orderProduct="orderDetail.orderProduct || {}"
     -->
    <phone-info :orderDetail="orderDetail" 
    v-if="orderDetail.orderProduct"
    :orderProduct="orderDetail.orderProduct"
     @handleAttr="handleAttr" />

    <!-- 订单信息 -->
    <order-info :orderDetail="orderDetail" />

    <view>
      <!-- 订单物流信息 -->
      <order-express v-if="orderDetail.orderExpress&&orderDetail.orderExpress.expressNo" :orderExpress="orderDetail.orderExpress" />
    </view>

    <!-- 操作按钮 -->
    <footer-btns :list="actionList" @handleBtnClick="handleBtnClick" />

    <view>
      <price-detail-layer ref="priceDetailRef" :orderDetail="orderDetail" />
    </view>
  </view>
</template>


<script>

import {
  getOrderDetail, getOrderActions,getOrderCancel
} from '@/api'
import OrderStatus from './info-components/OrderStatus.vue'
import UserInfo from './info-components/UserInfo.vue'
import PhoneInfo from './info-components/PhoneInfo.vue'
import OrderInfo from './info-components/OrderInfo.vue'
import CancelOrderLayer from './info-components/CancelOrderLayer.vue'
import FooterBtns from './info-components/FooterBtns.vue'
import OrderExpress from './info-components/OrderExpress.vue'
import PriceDetailLayer from './info-components/PriceDetailLayer.vue'

import { waitShowToast } from '@/utils'
export default {
  components: {
    OrderStatus,
    UserInfo,
    PhoneInfo,
    OrderInfo,
    CancelOrderLayer,
    FooterBtns,
    OrderExpress,
    PriceDetailLayer,
  },
  data() {
    return {
      orderNo: '',

      orderDetail: {
        orderAddress: {},
        orderProduct: {},
        orderExpress: {},
        orderNo:"",
        createdAt:"",
        orderTypeText:"",
      },

      actionList: [], // 订单操作按钮
    }
  },

  onLoad(options) {
    // options.id兼容老版本链接
    if (options.orderNo || options.id) {
      this.orderNo = options.orderNo || options.id
      this.loadDetail()
    }
  },

  onPullDownRefresh() {
    this.loadDetail()
  },

  methods: {
    loadDetail() {
      try {
        getOrderDetail({ orderNo: this.orderNo }).then(({ data, code }) => {
          if (code === 200) {
            this.orderDetail = data || {}

            this.loadOrderActions()
          }
        })
      } finally {
        uni.stopPullDownRefresh()
      }
    },

    // 加载订单操作按钮
    loadOrderActions() {
      getOrderActions({ orderNo: this.orderNo }).then(res => {
        if (res.code === 200) {
          this.actionList = res.data
        }
      })
    },

    // 按钮操作
    handleBtnClick(actionCode) {
      console.log('=====================>操作按钮:', actionCode)
      switch (actionCode) {
        case "cancel":
        this.handleCancel()
          break;
        case "again":
          this.handleAgain()
          break;
        default:
          console.log('---未定义操作---' + actionCode)
          break;
      }
    },
    handleAttr() {
      this.$refs.priceDetailRef.onOpen()
    },
    // 确认取消
    handleCancel() {
      const _this = this
      uni.showModal({
        title: '提示',
        content: '是否确认取消订单',
        success: function (res) {
          if (res.confirm) {
            console.log(_this.orderNo)
            getOrderCancel({
              remark: '客户端取消订单',
              orderNo: _this.orderNo,
            }).then(({ code, message }) => {
              if (code === 200) {
                _this.loadDetail()
                waitShowToast('取消成功')
              } else {
                waitShowToast(message)
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });

    },

    //重新估价
    handleAgain() {
      let url = '/pages/category/index'

      if (this.orderDetail.productId) {
        url = `/pages/product/index?id=${this.orderDetail.productId}`
      }

      uni.navigateTo({ url })
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  padding: 30rpx 30rpx 30rpx 30rpx;
  background-color: #f2f3f7;
  min-height: 100vh;
  /* #ifdef H5 */
  padding-top: calc(var(--status-bar-height) + 44px + 90rpx);
  /* #endif */
}
</style>